<?php
/**
 * Project: firework-cms             >>  http://firework-cms.googlecode.com
 * Authors: YouniS Ben Salah  (-.-)  >>  http://younishd.kegtux.org
 *          Fabien Wang       (^.^)  >>  http://fabienwang.fr
 * License: See README and LICENSE in the top level directory
 */
 
/**
 * Module: TemplateSystem [TS]
 * The template describes the appearance ( look n style ) of the webapp.
 * It's totally separated from the webapp, which allows extreme flexibility and modularity.
 * ( templates can be designed, regardless of the webapp that will use it later ).
 * Templates will be set from admin panel.
 */

require_once 'core/cl_config.php';

/**
 * Class: TemplateSystem
 * represents [TS] module.
 */
class TemplateSystem
{
    // ctor
    public function __construct( $templ = NULL )
    {
        $this->template = isset($templ) ? $templ : TEMPLATE_CONFIG::$DEFAULT_TEMPLATE;
    }
    
    /**
     * Method: setSnippet()
     */
    public function setSnippet( $id )
    {
        $this->snippet = $id;
        $this->dir = ( file_exists( 'templates'.DS.$this->template.DS.'snippets'.DS.$this->snippet.'.php' ) ? 'templates'.DS.$this->template.DS.'snippets'.DS.$this->snippet.'.php' : ( file_exists( 'snippets'.DS.$this->snippet.'.php' ) ? 'snippets'.DS.$this->snippet.'.php' : NULL ) );
        if ( $this->dir == NULL ) { throw new Exception( 'snippet does not exist. that\'s so sad.' ); }
    }

    /**
     * Method: getSnippet()
     */
    public function getSnippet()
    {
        return file_get_contents( $this->dir );
    }
    
    /**
     * Method: snippetExists()
     */
    public function snippetExists( $snippet )
    {
        return file_exists( 'templates'.DS.$this->template.DS.'snippets'.DS.$snippet.'.php' ) || file_exists( 'snippets'.DS.$snippet.'.php' );
    }
    
    /**
     * Method: getSnippetName()
     */
    //public function getSnippetName()
    //{
        //return $this->snippet;
    //}
    
    
    // current template
    protected $template;
    
    // current code snippet
    protected $snippet = NULL;
    
    // directory to use
    protected $dir;
}
